from building import *

CWD = GetCurrentDir()

objs = Glob('src/*.c')

@register_library
class LibraryCrc(Library):
    def gen(self):
        pkg = Package(url='https://pycrc.org/download/pycrc-0.9.2.zip')
        pkg = os.path.join(pkg, 'pycrc-0.9.2')
        MKDir('%s/src/crc16'%(CWD))
        cmd = '{0} {1}/pycrc.py --model crc-16 --poly 0x1021 --reflect-in False --reflect-out False --algorithm table-driven --generate h -o {2}/src/crc16/crc16.h'.format(sys.executable, pkg, CWD)
        RunCommand(cmd)
        cmd = '{0} {1}/pycrc.py --model crc-16 --poly 0x1021 --reflect-in False --reflect-out False --algorithm table-driven --generate c -o {2}/src/crc16/crc16.c'.format(sys.executable, pkg, CWD)
        RunCommand(cmd)
        MKDir('%s/src/crc32'%(CWD))
        cmd = '{0} {1}/pycrc.py --model crc-32 --poly 0x04C11DB7 --reflect-in True --reflect-out True --algorithm table-driven --generate h -o {2}/src/crc32/crc32.h'.format(sys.executable, pkg, CWD)
        RunCommand(cmd)
        cmd = '{0} {1}/pycrc.py --model crc-32 --poly 0x04C11DB7 --reflect-in True --reflect-out True --algorithm table-driven --generate c -o {2}/src/crc32/crc32.c'.format(sys.executable, pkg, CWD)
        RunCommand(cmd)

    def config(self):
        if (not os.path.exists('%s/src/crc16/crc16.c'%(CWD))):
            self.gen()
        self.CPPPATH = ['$INFRAS']
        self.source = objs

    
